package com.qsign.sfrz_android.activity.home.ViewController;

import android.content.Intent;
import android.device.ScanDevice;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hjq.permissions.Permission;
import com.qsign.sfrz_android.R;
import com.qsign.sfrz_android.activity.login.Model.MyUserData;
import com.qsign.sfrz_android.base.NewBaseActivity;
import com.qsign.sfrz_android.base.YcspUserDataShared;
import com.qsign.sfrz_android.net.IServiceImpl;
import com.qsign.sfrz_android.net.RequestServiceImpl;
import com.qsign.sfrz_android.publicview.SimpleDialog;
import com.qsign.sfrz_android.utils.BaseUtil;
import com.qsign.sfrz_android.utils.PermissionsUtil;
import com.qsign.sfrz_android.utils.ToolUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends NewBaseActivity {
    @BindView(R.id.login_accountEt)
    EditText loginAccountEt;
    @BindView(R.id.login_passwordEt)
    EditText loginPasswordEt;
    @BindView(R.id.versionlab)
    TextView versionlab;
    ScanDevice sm;
    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void initTitle(TextView textView) {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.login_activity;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (sm != null) {
            sm.stopScan();
            sm.setScanLaserMode(8);
            //sm.closeScan();
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        try {
            sm = new ScanDevice();
            sm.setOutScanMode(0);//启动就是广播模式
        } catch (Exception e) {
        }
        //请求清单文件中的所有权限
        PermissionsUtil.requestPersion(this, new String[]{
                Permission.REQUEST_INSTALL_PACKAGES, Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE});
    }

    @Override
    public void onPostHandle(int requestType, Object objBody, boolean error) throws Exception {
        stopWait();
        String msg = "";
        String opt = "";
        switch (requestType) {
            case IServiceImpl.LOGIN://根据条件查询
                try {
                    JSONObject jsonObject = JSONObject.parseObject(objBody.toString());
                    msg = jsonObject.getString("MSG_INFO");
                    opt = jsonObject.getString("OPT_FLAG");
                    if ("SUCCESS".equals(opt)) {
                        MyUserData myUserData=new MyUserData();
                        myUserData.setRYXXID(jsonObject.getString("RYXXID"));
                        myUserData.setXM(jsonObject.getString("XM"));
                        YcspUserDataShared.getInstance(LoginActivity.this).SaveUserBean(myUserData);
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    }else {
                        if (TextUtils.isEmpty(msg)) {
                            showSimpleDialog("查询出错");
                        } else {
                            showSimpleDialog(msg);
                        }

                    }
                } catch (Exception e) {
                }

                break;
        }
    }
    private void showSimpleDialog(String message) {
        SimpleDialog.show(this, message);
    }
    @OnClick(R.id.login_loginBtn)
    public void onViewClicked() {
        if(loginAccountEt.getEditableText().toString().length()==0){
            BaseUtil.ShowToast(this,"请输入工号");
            return;
        }
        if(loginPasswordEt.getEditableText().toString().length()==0){
            BaseUtil.ShowToast(this,"请输入密码");
            return;
        }

        showWait();
        RequestServiceImpl.login(mMIPHandler, IServiceImpl.LOGIN,loginAccountEt.getEditableText().toString(),ToolUtils.MD532(loginPasswordEt.getEditableText().toString()).toLowerCase());
    }
}
